import APPView from '@/views/APPView.vue'
import { createRouter, createWebHistory } from 'vue-router'
import NProgress from 'nprogress'
import { start, close } from '@/utils/nprogress'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      redirect: '/home'
    },
    {
      path: '/',
      name: 'app',
      component: APPView,
      children: [
        {
          path: '/home',
          name: 'home',
          component: () => import('../views/HomeView.vue'),
        },
        {
          path: '/virtual-accounts',
          name: 'virtualAccounts',
          component: () => import('../views/GlobalPayment/VirtualAccountsView.vue'),
        },
        {
          path: '/global-collection-payout',
          name: 'globalCollectionPayout',
          component: () => import('../views/GlobalPayment/CollectionPayoutView.vue'),
        },
        {
          path: '/stablecoin-checkout',
          name: 'stablecoinCheckout',
          component: () => import('../views/GlobalPayment/StablecoinCheckoutView.vue'),
        },
        {
          path: '/card',
          name: 'card',
          component: () => import('../views/GlobalPayment/Card.vue'),
        },
        {
          path: '/otc',
          name: 'otc',
          component: () => import('../views/OTC.vue'),
        },
        {
          path: '/contact',
          name: 'contact',
          component: ()=> import('../views/ContactView.vue')
        },
        {
          path: '/investment',
          name: 'investment',
          component: ()=> import('../views/MarketPlace/Investment.vue')
        },
        {
          path: '/how-work',
          name: 'howWork',
          component: ()=> import('../views/MarketPlace/HowWork.vue')
        },
        {
          path: '/how-select-project',
          name: 'howSelectProject',
          component: ()=> import('../views/MarketPlace/HowSelectProject.vue')
        },
        {
          path: '/career',
          name: 'career',
          component: () => import('../views/CareerView.vue'),
        },
        {
          path: '/aboutus',
          name: 'aboutus',
          component: () => import('../views/AboutUs.vue'),
        },
        {
          path: 'treasury',
          name: 'treasury',
          component: () => import('../views/UseCase/TreasuryView.vue'),
        },
        {
          path: 'import-export',
          name: 'importExport',
          component: () => import('../views/UseCase/ImportExportView.vue'),
        },
        {
          path: 'services',
          name: 'services',
          component: () => import('../views/UseCase/Services.vue'),
        },
        {
          path: 'venture-capital-investment-funds',
          name: 'ventureCapitalInvestmentFunds',
          component: () => import('../views/UseCase/VentureCapitalInvestmentFunds.vue'),
        },
        {
          path: 'crypto-native',
          name: 'cryptoNative',
          component: () => import('../views/UseCase/CryptoNative.vue'),
        },

        {
          path: 'province',
          name: 'province',
          component: () => import('../views/MarketPlace/Province.vue')
        }
      ],
    },
    {
      path: '/legal',
      name: 'legal',
      component: () => import('../views/LegalComplianceView.vue'),
    },
  ],
  scrollBehavior(to, from, savedPosition) {
    return to.path !== from.path ? { top: 0 } : {}
  }
})

// 路由守卫：在路由切换时触发进度条
router.beforeEach((to, from, next) => {
  start()
  next()
})

router.afterEach(() => {
  NProgress.done() // 结束进度条
})

// 错误处理
router.onError((error) => {
  close()
  console.error('路由错误:', error)
})

export default router
